package com.burgeon.backend.EnterpriseWeChat.rest.Analysis;

import com.burgeon.backend.EnterpriseWeChat.entity.VipRelegation;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class queryVipRelegation extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public queryVipRelegation(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<VipRelegation>  getData(String storeId, String content, int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(storeId,content,page), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<VipRelegation> vipRelegations= new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                VipRelegation vipRelegation = new VipRelegation();
                vipRelegation.setVipName(jsonArray.getJSONArray(i).getString(0));
                vipRelegation.setVipCardNo(jsonArray.getJSONArray(i).getString(1));
                vipRelegation.setUdType(jsonArray.getJSONArray(i).getString(2));
                String desc = jsonArray.getJSONArray(i).getString(3);
                String string = desc.substring(1, desc.length()).replace("到", "→");
                vipRelegation.setDescription(string);
                vipRelegation.setVipId(jsonArray.getJSONArray(i).getString(4));
                String logoUrl = jsonArray.getJSONArray(i).getString(5);
                if("null".equals(logoUrl)){
                    vipRelegation.setVipLogoUrl("http://lfwd.cloudsmicro.com:8080/img/EnterpriseWeChat/vip/app.png");
                }else {
                    vipRelegation.setVipLogoUrl(logoUrl);
                }
                vipRelegation.setVipPhone(jsonArray.getJSONArray(i).getString(6));
                vipRelegation.setVipOpenId(jsonArray.getJSONArray(i).getString(7));
                vipRelegation.setExternalUserId(jsonArray.getJSONArray(i).getString(8));
                System.err.println(vipRelegation);
                vipRelegations.add(vipRelegation);
            }
        }
        return vipRelegations;
    }

    private MultiValueMap Data(String storeId,String content,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'C_VIP_ID;VIPNAME'");
        colums.add("'C_VIP_ID;CARDNO'");
        colums.add("UD_TYPE");
        colums.add("DESCRIPTION");
        colums.add("C_VIP_ID");
        colums.add("'C_VIP_ID;LOGO_URL'");
        colums.add("'C_VIP_ID;MOBIL'");
        colums.add("'C_VIP_ID;OPENID2'");
        colums.add("'C_VIP_ID;EXTERNALUSERID'");//企业微信我的顾客标识

        params.put("columns", colums);
        params.put("table", "RFIM_VIP_UD");//表名
//        params.put("count", "true");

        params.put("range",RANGE);
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column","'C_STORE_ID'");
        String a = "'"+storeId+"'";
        expr1.put("condition",a);

        HashMap expr2 = new HashMap();
        String ac =  "'"+content+"'";
        expr2.put("column", "'C_VIP_ID;CARDNO'");
        expr2.put("condition", ac);

        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);
        paramss.put("combine","and");

        params.put("params", paramss);
        System.err.println(params);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        queryVipRelegation g = new queryVipRelegation("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("1515","",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
